package com.sywb.chuangyebao.view.fragment;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.contract.FollowContract;
import com.sywb.chuangyebao.utils.Utils;

import org.bining.footstone.log.Logger;
import org.bining.footstone.rxjava.rxbus.annotation.Subscribe;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;
import org.bining.footstone.utils.SharedUtils;

/**
 * 首页-关注
 */
public class FollowFragment extends BaseRefreshFragment<FollowContract.Presenter> implements FollowContract.View {

    private boolean isVisibleToTab = true;// 当前Fragemnt所在的Tab页是否可见
    private boolean isVisibleToUser = false;// 当前Fragemnt是否对用户可见

    /**
     * Fragment 实例
     */
    public static FollowFragment newInstance(Object... pramars) {
        FollowFragment fragment = new FollowFragment();
        fragment.setArguments(getBundle(pramars));
        return fragment;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        super.initView(view, bundle);
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.USERISLOGIN)})
    public void rxIsLogin(String isLogin) {
        Logger.e("收到登录状态改变广播：" + isLogin);
        if (SharedUtils.getBoolean(Constants.USERISLOGIN, false)) {
            if (mPresenter != null) {
                mPresenter.onStartRefresh();
            }
        } else {
            if (mPresenter != null) {
                mPresenter.exitLogin();
            }
        }
    }

    @Subscribe(thread = ThreadMode.MAIN_THREAD, tags = {@Tag(Constants.MAINTABCHANGE)})
    public void rxMainTabChange(String data) {
        Logger.e("rxMainTabChange：" + data);

        if (Integer.parseInt(data) == 0) {
            if (!isVisibleToTab) {// 之前所属Tab未显示
                isVisibleToTab = true;
                if (mPresenter != null) {
                    mPresenter.autoResume();
                }
            }
        } else {
            if (isVisibleToTab) {// 之前所属Tab未显示
                isVisibleToTab = false;
                if (mPresenter != null) {
                    mPresenter.autoPause();
                }
            }
        }
    }

    /**
     * 横竖屏切换
     *
     * @param newConfig
     */
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (isVisibleToTab && isVisibleToUser) {
            mPresenter.onConfigurationChanged(newConfig);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Logger.e("setUserVisibleHint：" + isVisibleToUser);
        this.isVisibleToUser = isVisibleToUser;
        if (mPresenter != null) {
            mPresenter.setUserVisibleHint();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10000 && mPresenter != null && data != null) {
            mPresenter.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        mPresenter.playStop();
    }

    @Override
    public void noDataClick() {
        if (!Utils.checkIsLogin()) {
            return;
        }
        super.noDataClick();
    }

    @Override
    public void setNoData(boolean isNoData) {
        if (isNoData && !SharedUtils.getBoolean(Constants.USERISLOGIN, false)) {
            commonNodata.setVisibility(View.VISIBLE);
            commonNodataIcon.setImageResource(R.drawable.nodata_default);
            commonNodataContent.setText("您还没有登录哦~");
            commonNodataSubtitle.setVisibility(View.VISIBLE);

            commonNodataSubtitle.setText("登录");
            commonNodataSubtitle.setBackgroundResource(R.drawable.round_pie_theme);
            commonNodataSubtitle.setTextColor(ContextCompat.getColor(mActivity, R.color.colorLight));
            commonNodataSubtitle.setPadding((int) getResources().getDimension(R.dimen.dp_24), (int) getResources().getDimension(R.dimen.dp_6), (int) getResources().getDimension(R.dimen.dp_24), (int) getResources().getDimension(R.dimen.dp_6));

            commonRefresh.setVisibility(View.GONE);
            return;
        } else {
            commonRefresh.setVisibility(View.VISIBLE);
        }
        super.setNoData(isNoData);
    }

    @Override
    public boolean getVisibleToTab() {
        return isVisibleToTab;
    }

    @Override
    public boolean getVisibleToUser() {
        return isVisibleToUser;
    }
}
